/*
Copyright (C) 2007 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
 * @param {DOMDocument} (either created manually or from an
 *                       XMLHttpRequest.responseXML)
 */
function SimpleXmlParser(xmlDoc) {
  this.xmlDoc = xmlDoc;
  this.parseError = xmlDoc.parseError;
  if (this.parseError.errorCode !== 0) {
    debug.error('SimpleXmlParser ERROR: ' + this.parseError.reason);
  }
}

/**
 * Get an array of specific items from the DOMDocument
 * @param {String} key is the name of the item(s) you are searching for
 * @param {String} parent filters only items children of an item named
 *                 parent (null for no filter)
 * @param {String} grandparent same as parent only refers to filtering
 *                 the item's father's father
 * @param {Integer} insertAncestry ancestors' names are added to the
 *                  structure of each item
 */
SimpleXmlParser.prototype.getItems = function(key, parent, grandparent,
                                              insertAncestry) {
  var xmlDoc = this.xmlDoc;
  var items = [];
  if (this.parseError.errorCode !== 0) {
     debug.error('SimpleXmlParser ERROR: ' + this.parseError.reason);
  } else {
    var objNodeList = xmlDoc.getElementsByTagName(key);
    for (var i = 0; i < objNodeList.length; ++i) {
      var xmlItem = objNodeList.item(i);
      var added = false;
      if (parent === null || xmlItem.parentNode.firstChild.text == parent) {
        if (grandparent === null ||
            xmlItem.parentNode.parentNode.firstChild.text == grandparent) {
          var item = {};
          for (var j = 0; j < xmlItem.childNodes.length; ++j) {
            var child = xmlItem.childNodes.item(j);
            if (child.childNodes.length > 0 || insertAncestry > 0) {
              if (child.childNodes.length > 0) {
                var name = child.nodeName;
                var value = child.childNodes[0].nodeValue;
                item[name] = value;
              }
              item['value'] = child.nodeValue;
              var ancValue = child.parentNode;
              var ancName = 'parent';
              for (var ancAux = 0; ancAux < insertAncestry; ++ancAux) {
                ancValue = ancValue.parentNode;
                if (ancValue === null) {
                  break;
                }
                item[ancName] = ancValue.firstChild.firstChild.text;
                ancName = 'grand' + ancName;
              }
              added = true;
            }
          }
          if (added) {
            items.push(item);
          }
        }
      }
    }
  }
  return items;
};
